package com.coderly.onlinegallerysharingplatform.common.oss.strategy.impl;

import cn.hutool.core.io.FileUtil;
import com.coderly.onlinegallerysharingplatform.common.oss.config.TencentOSSClientConfig;
import com.coderly.onlinegallerysharingplatform.common.oss.strategy.FileStrategy;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.*;
import com.qcloud.cos.model.ciModel.persistence.PicOperations;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 腾讯云对象存储实现类
 */
@Slf4j
public class TencentOSSFileStrategy implements FileStrategy {

    @Resource
    private TencentOSSClientConfig tencentOSSClientConfig;
    @Resource
    private COSClient cosClient;

    /**
     * 文件上传
     *
     * @param file 文件
     * @param key  唯一键，一个文档对应一个key
     * @return
     */
    @Override
    @Deprecated
    public Object uploadFile(File file, String key) {
        log.info("## 上传文件至 腾讯云OSS ...");
        String bucketName = tencentOSSClientConfig.getTencentOSSProperties().getBucket();
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file);
        return cosClient.putObject(putObjectRequest);
    }

    @Override
    @Deprecated
    public Object downloadFile(String key) {
        log.info(" ## 文件下载 ##");
        String bucketName = tencentOSSClientConfig.getTencentOSSProperties().getBucket();
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
        return cosClient.getObject(getObjectRequest);
    }


    /**
     * 远程调用腾讯云SDK 接口上传图片
     * @param file
     * @param key
     * @return
     */
    @Override
    public Object uploadPicture(File file, String key) {
        log.info("## 上传图片至 腾讯云OSS ...");
        String bucketName = tencentOSSClientConfig.getTencentOSSProperties().getBucket();
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file);
        // 对上传的图片进行处理
        PicOperations picOperations = new PicOperations();
        // 1 表示返回原图信息
        picOperations.setIsPicInfo(1);
        List<PicOperations.Rule> rules = new ArrayList<>();
        // 图片压缩（转成webpF）
        String webpKey = FileUtil.mainName(key) + ".webp";
        PicOperations.Rule compressRule = new PicOperations.Rule();
        compressRule.setRule("imageMogr2/format/webp");
        compressRule.setBucket(bucketName);
        compressRule.setFileId(webpKey);
        rules.add(compressRule);
        // 缩略图处理，仅对大于20KB图片生成缩略图
        if(file.length() > 20 * 1024) {
            String thumbnailKey = FileUtil.mainName(key) + "_thumbnail." + FileUtil.getSuffix(key);
            PicOperations.Rule thumbnailRule = new PicOperations.Rule();
            thumbnailRule.setRule(String.format("imageMogr2/thumbnail/%sx%s>", 256, 256));
            thumbnailRule.setFileId(thumbnailKey);
            thumbnailRule.setBucket(bucketName);
            rules.add(thumbnailRule);
        }

        // 构造处理参数
        picOperations.setRules(rules);
        putObjectRequest.setPicOperations(picOperations);
        // 发起上传图片请求
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
        // 删除上传的原图
        cosClient.deleteObject(bucketName, key);
        return putObjectResult;
    }

    /**
     * 远程调用腾讯云SDK 接口删除图片
     *
     * @param key
     * @return
     */
    @Override
    public Boolean deletePicture(String key) {
        log.info("## 腾讯云OSS 删除图片  ...");
        String bucketName = tencentOSSClientConfig.getTencentOSSProperties().getBucket();
        String key1 = key + ".webp";
        String key2 = key + "_thumbnail.png";
        cosClient.deleteObject(bucketName, key1);
        cosClient.deleteObject(bucketName, key2);
        return true;
    }


}